<?php


namespace broadcast\redis;

use broadcast\redis\RedisService;

class setRedis
{
    /**
     * 设置缓存
     * @param string $website 网站标识
     * @param string $key 缓存标识
     * @param array $data 缓存内容
     * @param $expire  default 0     过期时间
     */
    public static function setCache($website, $key, $data, $expire = 0)
    {
        if (strpos($key, 'mysql') !== false || strpos($key, 'info') !== false || strpos($key, 'default') !== false || strpos($key, 'redis') !== false) {
            //存在
            $redis = new RedisService(config('cache')['stores']['redis']);
        } else {
            // 不存在
            $redis = new RedisService(config('cache')['stores']['single']);
        }

        $nowkey = $website . ":" . $key;        //key
        $value = json_encode($data);        //值
        //判断过期时间是否存在

        if ($expire > 0) {
            $redis->setex($nowkey, $expire, $value);
        } else {
            $redis->set($nowkey, $value);
            // $websiteInfo = setRedis::getCache($website, "info");
            // if (isset($websiteInfo['overdue'])) {
            //     if ((int)$websiteInfo['overdue'] > '0') {
            //         setRedis::expireCache($nowkey, (int)$websiteInfo['overdue'] - time());
            //     } else {
            //         setRedis::expireCache($nowkey, 0);
            //     }
            // } else {
            //     setRedis::expireCache($nowkey, 0);
            // }
        }
    }

    /**
     * 获取缓存
     * @param string $website 网站标识
     * @param string $key 缓存标识
     */
    public static function getCache($website, $key)
    {
        if (strpos($key, 'mysql') !== false || strpos($key, 'info') !== false || strpos($key, 'default') !== false || strpos($key, 'redis') !== false) {
            //存在
            $redis = new RedisService(config('cache')['stores']['redis']);
        } else {
            // 不存在
            $redis = new RedisService(config('cache')['stores']['single']);
        }

        $nowkey = $website . ":" . $key;        //key
        //获取网站信息 并  设置数据过期时间

        $data = $redis->get($nowkey);

        if (!$data) return false;

        $data = json_decode($data, true);
        if (empty($data)) {
            return false;
        }

        return $data;
    }


    /**
     * 获取用户信息
     */
    public static function getUserCache($website, $key)
    {
        if (strpos($key, 'mysql') !== false || strpos($key, 'info') !== false || strpos($key, 'default') !== false || strpos($key, 'redis') !== false) {
            //存在
            $redis = new RedisService(config('cache')['stores']['redis']);
        } else {
            // 不存在
            $redis = new RedisService(config('cache')['stores']['single']);
        }
        $nowkey = $website . ":" . $key;
        $keys = $redis->keys($nowkey);
        if (count($keys) > 1) {
            $redis->del($keys);
            return false;
        } else if (count($keys) > 0) {
            $data = $redis->get($keys[0]);
            if (!$data) return false;
            $data = json_decode($data, true);
            if (empty($data)) {
                return false;
            } else {
                return ['data' => $data, 'key' => $keys[0]];
            }
        } else {
            return false;
        }
    }

    /**
     * 删除用户信息
     */
    public static function delUSerCache($website, $id)
    {

        $redis = new RedisService(config('cache')['stores']['single']);
        $idList = explode(",", $id);
        $idsLists = array();
        foreach ($idList as $value) {
            $key = $website . ":" . "usersList#" . $value . ":*";
            $item = $redis->keys($key);
            $idsLists = array_merge($idsLists, $item);
        }
        $redis->del($idsLists);
    }

    /**
     * 获取多个缓存
     */
    public static function getCaches($website, $key, $ids = [])
    {
        if (strpos($key, 'mysql') !== false || strpos($key, 'info') !== false || strpos($key, 'default') !== false || strpos($key, 'redis') !== false) {
            //存在
            $redis = new RedisService(config('cache')['stores']['redis']);
        } else {
            // 不存在
            $redis = new RedisService(config('cache')['stores']['single']);
        }
        $keyList = array();
        if (empty($ids)) {
            $nowkey = $website . ":" . $key . "*";        //key
            $keyList = $redis->keys($nowkey);
        } else {
            foreach ($ids as $value) {
                $keyList[] = $website . ":" . $key . $value;
            }
        }
        $list = array();
        foreach ($keyList as $value) {
            $item = $redis->get($value);
            if ($item) {
                $item = json_decode($item, true);
                if (!empty($item)) $list[] = $item;
            } else {
                $list[] = 0;
            }
            // halt($list);

        }
        if (empty($list)) return false;
        return $list;
    }

    /**
     * 删除缓存
     * @param string $website 网站标识
     * @param string $key 缓存标识
     */
    public static function delCache($website, $key)
    {
        if (strpos($key, 'mysql') !== false || strpos($key, 'info') !== false || strpos($key, 'default') !== false || strpos($key, 'redis') !== false) {
            //存在
            $redis = new RedisService(config('cache')['stores']['redis']);
        } else {
            // 不存在
            $redis = new RedisService(config('cache')['stores']['single']);
        }
        $nowkey = $website . ":" . $key;        //key
        $redis->del($nowkey);
    }

    /**
     * 删除缓存 指定key 下的所有缓存
     * @param string $website 网站标识
     * @param string $key 缓存标识
     */
    public static function delCaches($website, $key, $ids = [])
    {
        if (strpos($key, 'mysql') !== false || strpos($key, 'info') !== false || strpos($key, 'default') !== false || strpos($key, 'redis') !== false) {
            //存在
            $redis = new RedisService(config('cache')['stores']['redis']);
        } else {
            // 不存在
            $redis = new RedisService(config('cache')['stores']['single']);
        }
        $nowkey = $website . ":" . $key . "*";        //key
        $keyList = array();
        if (empty($ids)) {
            $nowkey = $website . ":" . $key . "*";        //key
            $keyList = $redis->keys($nowkey);
        } else {
            foreach ($ids as $value) {
                $keyList[] = $website . ":" . $key . $value;
            }
        }

        $redis->del($keyList);
    }

    /**
     * 删除一个网站的所有key
     */
    public static function delAll($website)
    {
        $redis = new RedisService(config('cache')['stores']['redis']);

        $keys = $website . ":*";

        $redis->del($redis->keys($keys));

        if (isset(config('cache')['stores']['single'])) {
            if (!empty(config('cache')['stores']['single'])) {
                $redis = new RedisService(config('cache')['stores']['single']);
                $keys = $website . ":*";
                $redis->del($redis->keys($keys));
            }
        }
    }

    /**
     * redis 设置过期时间
     */
    public static function expireCache($key, $time)
    {
        if (strpos($key, 'mysql') !== false || strpos($key, 'info') !== false || strpos($key, 'default') !== false || strpos($key, 'redis') !== false) {
            //存在
            $redis = new RedisService(config('cache')['stores']['redis']);
        } else {
            // 不存在
            $redis = new RedisService(config('cache')['stores']['single']);
        }
        $now = $time + (60 * 60 * 24 * 7);
        $redis->expire($key, $now);
    }

    /**
     * 更新所有房间信息
     */
    public static function editRooms($website, $key, $data)
    {
        if (strpos($key, 'mysql') !== false || strpos($key, 'info') !== false || strpos($key, 'default') !== false || strpos($key, 'redis') !== false) {
            //存在
            $redis = new RedisService(config('cache')['stores']['redis']);
        } else {
            // 不存在
            $redis = new RedisService(config('cache')['stores']['single']);
        }
        $nowkey = $website . ":" . $key . "*";        //key

        //获取所有key
        $keyList = $redis->keys($nowkey);

        foreach ($keyList as $value) {
            $item = $redis->get($value);
            $item = json_decode($item, true);
            foreach ($data as $k => $v) {
                $item[$k] = $v;
            }
            $redis->set($value, $item);
        }
    }

    public static function getRoomsData($website, $key, $data)
    {
        if (strpos($key, 'mysql') !== false || strpos($key, 'info') !== false || strpos($key, 'default') !== false || strpos($key, 'redis') !== false) {
            //存在
            $redis = new RedisService(config('cache')['stores']['redis']);
        } else {
            // 不存在
            $redis = new RedisService(config('cache')['stores']['single']);
        }
        $key = $website . ":" . $key;
        $data = $redis->hMget($key, $data);
        if (!$data) return false;
        return $data;
    }

    public static function getRoomsKeys($website, $key)
    {
        if (strpos($key, 'mysql') !== false || strpos($key, 'info') !== false || strpos($key, 'default') !== false || strpos($key, 'redis') !== false) {
            //存在
            $redis = new RedisService(config('cache')['stores']['redis']);
        } else {
            // 不存在
            $redis = new RedisService(config('cache')['stores']['single']);
        }

        $nowkey = $website . ":" . $key . "*";        //key
        //获取网站信息 并  设置数据过期时间

        $data = $redis->keys($nowkey);

        foreach ($data as &$item) {
            $item = str_replace($website . ":" . $key, "", $item);
        }
        unset($data[array_search("888", $data)]);

        return $data;
    }

    /**
     * 记录每日房间注册数量
     * @param $website
     * @param $key
     * @param $roomid
     */
    public static function addRegisterCount($website, $key, $roomid)
    {
        $redis = new RedisService(config('cache')['stores']['single']);
        $key = $website . ":RegisterCount#" . $key;
        $redis->hIncrBy($key, $roomid, 1);
    }

    /**
     * 获取每日房间注册数量
     * @param $website
     * @param $key
     * @param $roomid
     * @return false|int|string
     */
    public static function getRegisterCount($website, $key, $roomid)
    {
        $redis = new RedisService(config('cache')['stores']['single']);
        $key = $website . ":RegisterCount#" . $key;
        $nums = $redis->hGet($key, $roomid);
        if ($nums) {
            return $nums;
        }
        return 0;
    }

    /**
     * 新增房间统计数据缓存
     */
    public static function insertRoomsData($website, $key, $field, $value)
    {
        if (strpos($key, 'mysql') !== false || strpos($key, 'info') !== false || strpos($key, 'default') !== false || strpos($key, 'redis') !== false) {
            //存在
            $redis = new RedisService(config('cache')['stores']['redis']);
        } else {
            // 不存在
            $redis = new RedisService(config('cache')['stores']['single']);
        }
        $key = $website . ":" . $key;
        $redis->hSet($key, $field, $value);
    }

    /**
     * 房间数据更改
     */
    public static function changeRoomsData($type, $website, $key, $field, $data)
    {

        if (strpos($key, 'mysql') !== false || strpos($key, 'info') !== false || strpos($key, 'default') !== false || strpos($key, 'redis') !== false) {
            //存在
            $redis = new RedisService(config('cache')['stores']['redis']);
        } else {
            // 不存在
            $redis = new RedisService(config('cache')['stores']['single']);
        }

        $key = $website . ":" . $key;
        $findValue = $redis->hGet($key, $field);
        if ($findValue) {
            $findValue = json_decode($findValue, true)??[];
            foreach ($data as $k => $item) {
                if (isset($findValue[$k])) {
                    if ($type == 'incr') {
                        $findValue[$k] += $item;
                    } else {
                        $findValue[$k] -= $item;
                        if ($findValue[$k] <= 0) {
                            $findValue[$k]  = 0;
                        }
                    }
                } else {
                    if ($type == 'incr') {
                        $findValue[$k] = $item;
                    } else {
                        $findValue[$k] = 0;
                    }
                }
            }
            $findValue = json_encode($findValue);
            $redis->hSet($key, $field, $findValue);
        } else {
            if ($type == 'incr') {
                $data = json_encode($data);
                $redis->hSet($key, $field, $data);
            }
        }
    }


    public static function prohibitiplist($ip)
    {
        $redis = new RedisService(config('cache')['stores']['redis']);

        $key =  'prohibitiplist';

        $data = $redis->get($key);

        if (!$data) return $ip;

        $data = json_decode($data, true);
        if (empty($data)) {
            return $ip;
        }
        if (in_array($ip,$data)) {
            return '';
        }else{
            return $ip;
        }


    }

}




/**
 * 网站数据库          zhibo1:mysql                            网站标识 + key名称
 *
 * 网站信息            zhibo1:info                             网站标识 + key名称
 *
 * 网站配置            zhibo1:default                          网站标识 + key名称
 *
 * 网站房间互通        zhibo1:nowInterWork                      网站标识 + key名称
 *
 * 网站banner          zhibo1:bannList                         网站标识 + key名称
 *
 * 网站房间            zhibo1:roomInfo:1                       网站标识 + key名称 + 房间id
 *
 * 用户标识            zhibo1:usersList#admin:1                网站标识 + key名称 + 用户id 用户名称
 *
 * 讲师列表            zhibo1:lecturerList                     网站标识 + key名称
 *
 */
